package com.plum.admin.modules.message.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.exception.BaseException;
import com.plum.admin.common.util.SecurityUtil;
import com.plum.admin.modules.message.entity.SysMessage;
import com.plum.admin.modules.message.entity.SysMessageSend;
import com.plum.admin.modules.message.mapper.SysMessageMapper;
import com.plum.admin.modules.message.service.ISysMessageSendService;
import com.plum.admin.modules.message.service.ISysMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plum.admin.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 子丶沫
 * @since 2021-09-24
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {
    @Autowired
    private ISysMessageSendService messageSendService;
    @Autowired
    private SecurityUtil securityUtil;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendMessage(SysMessage message) {
        int insert = baseMapper.insert(message);
        if(insert>0){
            SysUser user = securityUtil.getUser();
            List<String> receiveIdList = message.getReceiveIdList();
            List<SysMessageSend> messageSendList = receiveIdList.stream().map(id -> {
                SysMessageSend messageSend = new SysMessageSend();
                messageSend.setMessageId(message.getId());
                messageSend.setSenderId(user.getId());
                messageSend.setMessageType(message.getMessageType());
                messageSend.setMessageTitle(message.getMessageTitle());
                messageSend.setContent(message.getContent());
                messageSend.setReceiveId(id);
                messageSend.setStatus(Constant.UNREAD);
                return messageSend;
            }).collect(Collectors.toList());
           return messageSendService.batchInsert(messageSendList)>0;
        }
        return false;
    }

    @Override
    public boolean updateMessage(SysMessage message) {
        int insert = baseMapper.updateById(message);
        Boolean isResend = message.getIsResend();
        if(!isResend){
            return insert>0;
        }
        boolean b = messageSendService.remove(Wrappers.<SysMessageSend>lambdaUpdate().eq(SysMessageSend::getMessageId, message.getId()));
        if(!b){
            throw new BaseException("重新发送失败");
        }
        SysUser user = securityUtil.getUser();
        List<String> receiveIdList = message.getReceiveIdList();
        List<SysMessageSend> messageSendList = receiveIdList.stream().map(id -> {
            SysMessageSend messageSend = new SysMessageSend();
            messageSend.setMessageId(message.getId());
            messageSend.setSenderId(user.getId());
            messageSend.setMessageType(message.getMessageType());
            messageSend.setMessageTitle(message.getMessageTitle());
            messageSend.setContent(message.getContent());
            messageSend.setReceiveId(id);
            messageSend.setStatus(Constant.UNREAD);
            return messageSend;
        }).collect(Collectors.toList());
        return messageSendService.batchInsert(messageSendList)>0;
    }
}
